package com.greenphosphor.glasshouse;

import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.util.logging.*;

public class CICPMsgRelay extends HttpServlet {

  private Logger log;

  public void init () throws ServletException {
    log = Logger.getLogger("MSGRELAY");
  }

  protected void doGet ( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
    try {
      res.setContentType("text/html");
      PrintWriter pw = res.getWriter();
      String msg = req.getParameter("msg");
      String host = req.getParameter("host");
      Integer port = Integer.parseInt(req.getParameter("port"));

      String rline = null;
      Socket s = new Socket(host, port);
      PrintWriter pwg = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
      BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
      pwg.println(msg);
      pwg.flush();
      rline = br.readLine();
      int i = rline.indexOf("glasshouse.greenphosphor.com");
      if ( i > -1 )
        rline = rline.substring(0, i) + "209.160.40.247" + rline.substring(i + 28);
      pwg.close();
      br.close();
      pw.println(rline);
      pw.close();
    }
    catch ( IOException ie ) {
      throw new ServletException("error: " + ie, ie);
    }
  }
}
